class Solution
{
public:
    long long minimumPerimeter(long long neededApples)
    {
        auto amount = [](long long n) -> long long
        {
            return 2 * n * (n + 1) * (n * 2 + 1);
        };
        long long l = 0;
        long long r = 1e6;
        while (l < r)
        {
            long long mid = (l + r) / 2;
            if (amount(mid) >= neededApples)
            {
                r = mid;
            }
            else
            {
                l = mid + 1;
            }
        }
        return l * 8;
    }
};